package com.ff.neuropp.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.ff.neuropp.domain.Career;
import com.ff.neuropp.domain.CareerType;
import com.ff.neuropp.domain.Course;
import com.ff.neuropp.repository.CareerRepository;
import com.ff.neuropp.repository.CareerTypeRepository;
import com.ff.neuropp.repository.CourseRepository;
import com.ff.neuropp.repository.ResourceRepository;
import com.ff.neuropp.response.CareerInfo;
import com.ff.neuropp.response.ResourceMoreRelated;
import com.ff.neuropp.response.ResourceResponse;
import com.ff.neuropp.util.DateUtils;
import com.ff.neuropp.util.ResultTool;

@Service
public class AppCareerService {

	@Resource
	private CareerTypeRepository careerTypeDao;

	@Resource
	private CareerRepository careerDao;

	@Resource
	private CourseRepository courseDao;

	@Resource
	private ResourceRepository resourceDao;

	interface STATUS {
		// 上线
		Integer ON = 1;
		// 下线
		Integer OFF = 0;
	}

	/**
	 * 职业生涯分类
	 * 
	 * @param name
	 * @return
	 */
	public Map<String, Object> careerType(String name) {

		List<CareerInfo> infos = new ArrayList<>();
		List<CareerType> list = new ArrayList<>();
		if (StringUtils.isNotEmpty(name)) {
			list = careerTypeDao.findAllByStatusAndCareerTypeNameLike(STATUS.ON, "%" + name + "%");
		} else {
			list = careerTypeDao.findAllByStatus(STATUS.ON);
		}
		if (CollectionUtils.isNotEmpty(list)) {
			list.forEach(o -> {
				CareerInfo a = new CareerInfo(o.getId(), o.getCareerTypeName());
				infos.add(a);
			});
			return ResultTool.genSuccessResult(infos);
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}

	/**
	 * 职业生涯列表
	 * 
	 * @param typeId
	 * @return
	 */
	public Map<String, Object> careerList(int typeId) {

		CareerType type = new CareerType();
		type.setId(typeId);
		List<Career> list = careerDao.findAllByCareerType(type);
		if (CollectionUtils.isNotEmpty(list)) {
			return ResultTool.genSuccessResult(list);
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}
	
	
	 /**
	   * 根据职业生涯ID 查询职业生涯详情
	 * @param careerId
	 * @return
	 */
	public Map<String, Object> careeDetail(Integer careerId) {
		Optional<Career> info = careerDao.findById(careerId);
		if (info.isPresent()) {
			Career career = info.get();
			return ResultTool.genSuccessResult(career);
		}
		return ResultTool.genSuccessResult("未查询到职业生涯详情");
	
	}

	/**
	 * 职业生涯对应课程
	 * 
	 * @param carrerId
	 * @return
	 */
	public Map<String, Object> careerCourses(int carrerId) {
		List<Course> list = courseDao.findAllByStatusAndCareerId(STATUS.ON, String.valueOf(carrerId));
		if (CollectionUtils.isNotEmpty(list)) {
			return ResultTool.genSuccessResult(list);
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}

	/**
	 * 职业生涯资源列表
	 * 
	 * @param carrerId
	 * @param carrerId
	 * @return
	 */
	public Map<String, Object> resourceList(int carrerId) {

		Career career = new Career();
		career.setId(carrerId);
		List<com.ff.neuropp.domain.Resource> list = resourceDao.findAllByCareer(career);
		if (CollectionUtils.isNotEmpty(list)) {
			return ResultTool.genSuccessResult(list);
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}

	/**
	 * 职业生涯详情
	 * 
	 * @param resourceId   资源ID
	 * @param careerId     职业生涯ID
	 * @param resourceTime 资源时间 筛选
	 * @return
	 */
	public Map<String, Object> resourceDetail(Integer resourceId, Integer careerId) {
		ResourceResponse resourceResponse = new ResourceResponse();
		Optional<com.ff.neuropp.domain.Resource> optional = resourceDao.findById(resourceId);
		if (optional.isPresent()) {
			com.ff.neuropp.domain.Resource resourse = optional.get();
			resourceResponse.setId(resourse.getId());
			resourceResponse.setResourceName(resourse.getResourceName());
			resourceResponse.setResourceAbout(resourse.getResourceAbout());
			resourceResponse.setResourceDesc(resourse.getResourceDesc());
			resourceResponse.setResourceTitle(resourse.getResourceTitle());
			resourceResponse.setResourceTime(resourse.getResourceTime());
			resourceResponse.setResourceDate(resourse.getResourceDate());
			resourceResponse.setResourceLocal(resourse.getResourceLocal());
			resourceResponse.setCareer(resourse.getCareer());
			resourceResponse.setFileList(resourse.getFileList());
			resourceResponse.setCreate_date(resourse.getCreate_date());
			resourceResponse.setUpdateTime(resourse.getUpdateTime());
			Career career = new Career();
			List<ResourceMoreRelated> moreRelateList = new ArrayList<ResourceMoreRelated>();
			if (!org.springframework.util.StringUtils.isEmpty(careerId)) {
				career.setId(careerId);
				List<com.ff.neuropp.domain.Resource> list = resourceDao.findAllByCareer(career);
				if (list.size() > 0) {
					for (com.ff.neuropp.domain.Resource resource : list) {
						ResourceMoreRelated moreRelated = new ResourceMoreRelated();
						moreRelated.setResourceId(resource.getId());
						moreRelated.setResourceName(resource.getResourceName());
						moreRelated.setResourceTime(resource.getResourceTime());
						moreRelateList.add(moreRelated);
					}
				}

			}
			resourceResponse.setMoreRelateList(moreRelateList);
			resourceResponse.setArticleList(resourse.getArticleList());
			return ResultTool.genSuccessResult(resourceResponse);
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}

	/**
	 * 根据资源ID和资源时间查询出当前数据
	 * @param resourceId
	 * @param resourceTime
	 * @return
	 */
	public Map<String, Object> resourceClickDetail(Integer resourceId, String resourceDate) {
		com.ff.neuropp.domain.Resource resourceResponse;
		try {
			Date date=DateUtils.getDate(resourceDate, DateUtils.FORMAT_DATE);
			resourceResponse = resourceDao.findAllByIdAndResourceDate(resourceId,date);
			return ResultTool.genSuccessResult(resourceResponse);
		} catch (Exception e) {
		}
		return ResultTool.genFailResult(ResultTool.ErrorCode_1, "未查询到数据");
	}

}
